package com.smgeek.gkrpc.transport;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.jetty.http.HttpParser;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * @ClassName HttpTransportServer
 * @Author wuyuyang
 * @Date 2022/1/6 17:09
 * @Description HttpTransportServer
 * @Version 1.0
 */
@Slf4j
public class HttpTransportServer implements TransportServer {
    private RequestHandler handler;
    private Server server;


    @Override
    public void init(int port, RequestHandler handler) {

        this.handler = handler;
        this.server = new Server(port);

        // servlet 接受请求
        ServletContextHandler ctx = new ServletContextHandler();
        server.setHandler(ctx);

        ServletHolder holder = new ServletHolder(new RequestServlet());
        ctx.addServlet(holder,"/*");
    }

    @Override
    public void start()  {
        try {
            server.start();
            //让server一直挂起
            server.join();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(), e);

        }
    }

    @Override
    public void stop() {
        try {
            server.stop();
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
    }

    class  RequestServlet extends HttpServlet {
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            log.info("get request success");
            ServletInputStream in = req.getInputStream();
            ServletOutputStream out  = resp.getOutputStream();
            if(handler!=null){
                handler.onRequest(in,out);
            }

            out.flush();
        }
    }
}
